package com.bjpowernode.springboot.web;

import com.bjpowernode.springboot.domain.User;
import com.bjpowernode.springboot.mapper.UserMapper;
import com.bjpowernode.springboot.service.TestService;
import com.bjpowernode.springboot.util.ExcelExport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Copyright (C), 2021-2021, 颜子初
 * Author: yan
 * Date: 2021/11/15 9:55
 * FileName: ExcelOutController
 * Description: 导出excel第二种
 */
@RestController
@RequestMapping("/excelout")
public class ExcelOutController {
    @Autowired
    TestService testService;
    /**
     * description: excel 数据导出
     * @return String
     * @version v1.0
     * @author w
     * @throws IOException
     * @date 2020年3月31日 下午2:41:03
     */
    @GetMapping(value= "/export")
    public String export(HttpServletResponse response) throws IOException {

        String[] headers = new String[] {"id","姓名","性别","手机","密码"};
        List<List<Object>> dataList = new ArrayList<>();
        List<User> userList=testService.personServiceExcel();
        for(int x = 0 ; x<userList.size(); x++) {
            List<Object> data = new ArrayList<Object>();
            data.add(userList.get(x).getId());
            data.add(userList.get(x).getName());
            data.add(userList.get(x).getSex());
            data.add(userList.get(x).getPhone());
            data.add(userList.get(x).getPassword());
            dataList.add(data);
        }
        try {
            ExcelExport.export("用户数据", headers, dataList,"poi导出模板.xlsx" ,response);
            return null ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "导出失败 --";
    }
}
